//
//  SFStatusViewModel.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/12.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit

class SFStatusViewModel: NSObject {


    var status: SFStatus?

    //真实来源
    var dealSource: String?
    //处理过的时间
    var creatTimeText: String?{

        get{

            return status?.creatTime?.dateDescription
        }
    }

    //加v地址
    var verifiedImage: UIImage?

    var vipImage: UIImage?

    //消息数
    var reposts:String?
    //转发数
    var comments:String?
    //点赞数
    var attitudes:String?

    //转发的文本
    var retweelString: String?

    init(status: SFStatus) {

        super.init()

        self.status = status

        //设置加V图片

        let verifiedType = status.user?.verified ?? 0

        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }


        //设置会员图标
        let vipRank = status.user?.mbrank ?? 0

        var vipImageName: String?

        if vipRank != 0 {
            vipImageName = String("common_icon_membership_level\(vipRank)")
        } else{

            vipImageName = String("common_icon_membership")
        }

        vipImage = UIImage(named: vipImageName!)


        //消息数
        reposts = stringWithCount(count: status.reposts_count, defaultString: "评论")

        //转发数
        comments = stringWithCount(count: status.comments_count, defaultString: "转发")

        //点赞数
        attitudes = stringWithCount(count: status.attitudes_count, defaultString: "赞")



        //转发的文本
        if status.retweeted_status?.user?.screen_name != nil && status.retweeted_status?.text != nil{

            retweelString = "@\(status.retweeted_status!.user!.screen_name!): \(status.retweeted_status!.text!)"
        }else {

            retweelString = "微博已被删除"
        }


        //真实来源
        dealSource = {

            // "source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",

            guard let s: String = status.source else{
                return nil
            }
            //截取字符串
            if let start = s.rangeOfString("\">")?.endIndex {

                if let end = s.rangeOfString("</")?.startIndex{

                    return s.substringWithRange(start..<end)
                }
            }
            return s
        }()

        //处理时间
//        creatRealTimeText()

    }


//    private func creatRealTimeText(){
//
//        //取到日期
//        guard let date = status?.creatTime else {
//
//            return
//        }
//
//        //当前时间
//        var currentTime = NSDate()
//
//        //测试时间
////        date = NSDate().dateByAddingTimeInterval(-60*60*24*30*12)
//        //定义格式
//        let fm = NSDateFormatter()
//        fm.locale = NSLocale(localeIdentifier: "en")
//
//        //取到日历对象
//        let calendar = NSCalendar.currentCalendar()
//
//        //判断是否是今年
//        if isDataInThisYear(date) {
//
//            if calendar.isDateInToday(date) {
//                //今天
//                //取得秒差
//                let result = -date.timeIntervalSinceNow
//                //判断是不是1分钟以内
//                if result < 60 {
//                    creatTimeText = "刚刚"
//                } else if result < 3600{
//                    //判断是不是1小时以内
//                    creatTimeText = "\(Int(result/60))分钟前"
//                } else {
//                    //其他
//                    creatTimeText = "\(Int(result/60/60))小时前"
//                }
//
//            } else if calendar.isDateInYesterday(date) {
//                //昨天
//                fm.dateFormat = "HH:mm"
//                creatTimeText = "昨天:\(fm.stringFromDate(date))"
//
//            } else {
//                fm.dateFormat = "MM-dd HH:mm"
//                creatTimeText = "\(fm.stringFromDate(date))"
//            }
//
//
//        } else {
//            //不是今年
//            fm.dateFormat = "yyyy-MM-dd"
//            creatTimeText = fm.stringFromDate(date)
//        }
//
//
//    }




    //返回消息数啥的,数一类的
    private func stringWithCount(count count: Int, defaultString: String) -> String{


        //当1-10000 显示全部
        //10000以上显示万位和千位


        if count < 10000 {

            if count == 0 {

                return defaultString

            }else{

                return String(count)
            }

        }else{


            let string = String("\(CGFloat(count / 1000) / 10)万")

            if string.containsString(".0") {

                return string.stringByReplacingOccurrencesOfString(".0", withString: "")
            }

            return string
        }

    }

}
